import api from "../../utils/api_util";
import baseUtil from "../../utils/base_util";
import tips from "../../utils/tip";

const app = getApp();
Page({
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        images: [],
        reason:'',
        number:'',
        detailData:'',
        baseUrl:'',
    },
    onLoad: function (options) {

    },
    onShow:function(){
       this.getOrderDetail();

    },
    setScores: function (e) {
        var id = e.currentTarget.dataset.id;
        var index = e.currentTarget.dataset.index;
        this.data.rating.scores = index;
        console.log(this.data.rating);
        this.setData({
            rating: this.data.rating
        })
    },
    textareaChange:function(e){
         this.setData({
             reason:e.detail.value,
         })
    },
    inputChange:function(e){
        this.setData({
            number:e.detail.value,
        })
    },
    chooseImage(e) {
        let that =this;
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success: res => {

                that.doUpload(res.tempFilePaths);
            }
        })
    },
    doUpload: function(tempFilePaths) {
        let that =this;
        console.log("tempFilePaths:",tempFilePaths)
        wx.uploadFile({
            url: api.urls.fileUpload,
            filePath: tempFilePaths[0],
            name: 'file',
            header:{
                'token': baseUtil.getStorageSync('token') ? baseUtil.getStorageSync('token') : '',
            },
            success(res) {
                let data =JSON.parse(res.data);
                let images = [...that.data.images, api.urls.imgUrl+data.data.file[0]];
                that.setData({
                    images: images
                });
                console.log("filefilefile__:"+data.data.file)

            },
        })
    },

    removeImage(e) {
        let iindex = e.target.dataset.index;
        this.setData({
            images: this.data.images.filter((item, index) => index != iindex)
        })
        wx.showToast({
            title: "图片已删除"
        })
    },
    //申请补发
    orderApplyPost: function () {
        let that = this;
        api.wxRequest.apiRequest(that, api.urls.orderApply, {
            method: 'POST',
            data: {
                orderId: baseUtil.getCurrentUrlParam('orderId'),
                pics: that.data.images[0],
                remark: '',
                reason: that.data.reason,
                number: that.data.number,
            },
            success: function (res) {

                setTimeout(() => {
                    tips.alert('申请成功~');
                }, 100);

                baseUtil.pageJump('/pages/OrderDetail/OrderDetail?orderId=' + baseUtil.getCurrentUrlParam('orderId'))


            },
        })
    },
    //订单详情
    getOrderDetail() {
        let that = this;
        api.wxRequest.apiRequest(that, api.urls.orderDetail, {
            method: 'GET',
            data: {
                id:baseUtil.getCurrentUrlParam('orderId'),
            },
            success: function (res) {

                res.data.createTime = baseUtil.formatDateTime(res.data.createTime);

                that.setData({
                    detailData:res.data
                })
            },
        })
    },
})